package com.ktjiaoyu.crm;

import com.ktjiaoyu.crm.pojo.Role;
import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.repository.RoleRepository;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
//@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTester {
//    @Resource
//    private UserRepository userRepository;
    @Resource
    private RoleRepository roleRepository;

    @Test
    public void testAdd(){//测试级联新增
     Role role=new Role("测试角色","演示级联新增角色和用户",1);
     User user1=new User("测试用户1","123456",role,1);
     User user2=new User("测试用户2","123456",role,1);
     role.getUsers().add(user1);
     role.getUsers().add(user2);
     roleRepository.save(role);//新增角色的同时新增关联的用户
    }

    //级联删除
    @Test
    public void testDelete() {//测试级联删除
        //先使用getOne方法获取到Role的引用，然后调用delete方法删除
        Role role = roleRepository.getOne(9L);
        roleRepository.delete(role);
    }
    @Test
    public void testsGet(){//测试按主键查询角色,输出关联角色数量
        Role role=roleRepository.findById(2L).get();
        System.out.println("roleName "+role.getRoleName());
        System.out.println("users.size "+role.getUsers().size());
    }

//    @Test
//    public void testGet(){//测试按注解查询用户,关联获得
//        User user=userRepository.findById(2L).get();
//        System.out.println("usrName "+user.getUsrName());
//        System.out.println("roleName "+user.getRole().getRoleName());
//    }


  //    @Resource
  //    private UserService userService;


//    @Test
//    public void testInsert(){
//        User user = new User("JPA","123456",2L,1);
//        userRepository.save(user);
//    }
//
//    @Test
//    public void testGet(){
//        User user = userRepository.findById(6L).get();
//        System.out.println("usrName："+user.getUsrName());
//    }
//
//    @Test
//    public void testFindByUsrNameLike(){
//        List<User> list = userRepository.findByUsrNameLike("%s%");
//        if (list != null){
//            for (User user : list){
//                System.out.println("usrName："+user.getUsrName());
//            }
//        }
//    }
//
//
//
//    @Test
//    public void testFindPageByUsrRoleId(){
//        int page=0,size=4;
//        Sort sort = Sort.by(Sort.Direction.DESC,"usrId");
//        Pageable pageable = PageRequest.of(page,size,sort);
//        Page<User> userPage = userRepository.findPageByUsrRoleId(9L,pageable);
//        System.out.println("总记录数："+userPage.getTotalElements());
//        System.out.println("总页数："+userPage.getTotalPages());
//        System.out.println("当前页数："+userPage.getNumber()+1);
//        System.out.println("每页记录数："+userPage.getSize());
//        System.out.println("当前页记录数："+userPage.getNumberOfElements());
//        for (User user : userPage.getContent()){
//            System.out.println("usrId："+user.getUsrId());
//        }
//    }
//
//
//
//
//    @Test
//    public void testFindPageByMap(){
//        int page=0,size=5;
//        Sort sort = Sort.by(Sort.Direction.DESC,"usrId");
//        Pageable pageable = PageRequest.of(page,size,sort);
//        Map param = new HashMap();
//        param.put("roleId",3L);
//        Page<User> userPager = userService.findPageByMap(param,pageable);
//        System.out.println("总记录数 -----> "+userPager.getTotalElements());
//        System.out.println("总页数 -----> "+userPager.getTotalPages());
//        System.out.println("当前页数-----> "+userPager.getNumber());
//        System.out.println("当前记录数 -----> "+userPager.getSize());
//        System.out.println("当前页记录 -----> ");
//        for (User user : userPager.getContent()){
//            System.out.println("usrId："+user.getUsrId());
//        }
//    }
}